home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / transpor / ifmail23.z / ifmail23 / ifmail / ifcico / sendbark.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-10  |  2.4 KB  |  151 lines

  1. #include <unistd.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include "ftn.h"
  6. #include "lutil.h"
  7. #include "ttyio.h"
  8. #include "session.h"
  9. #include "statetbl.h"
  10.  
  11. extern unsigned short crc16(char*,int);
  12. extern char *reqname(faddr*);
  13. extern int xmrecv(char*);
  14.  
  15. static int send_bark(void);
  16. static char *nm,*pw,*dt;
  17.  
  18. int sendbark(void);
  19. int sendbark(void)
  20. {
  21.     char *fn;
  22.     FILE *fp;
  23.     char buf[256],*p;
  24.     int rc=0;
  25.  
  26.     fn=reqname(remote->addr);
  27.     if ((fp=fopen(fn,"r")) == NULL)
  28.     {
  29.         debug(10,"no request file for this node");
  30.         PUTCHAR(ETB);
  31.         return 0;
  32.     }
  33.     while (fgets(buf,sizeof(buf)-1,fp))
  34.     {
  35.         nm=buf;
  36.         pw=strchr(buf,'!');
  37.         /* if ((dt=strchr(buf,'-')) == NULL) */
  38.         dt=strchr(buf,'+');
  39.  
  40.         if (pw) *pw++='\0';
  41.         if (dt) *dt++='\0';
  42.  
  43.         if (nm)
  44.         {
  45.             while (isspace(*nm)) nm++;
  46.             for (p=nm;(*p != '!') && (*p != '+') && 
  47.                 (!isspace(*p));p++);
  48.             *p='\0';
  49.         }
  50.         if (pw)
  51.         {
  52.             while (isspace(*pw)) pw++;
  53.             for (p=pw;(*p != '!') && (*p != '+') && 
  54.                  (!isspace(*p));p++);
  55.             *p='\0';
  56.         }
  57.         else pw="";
  58.         if (dt)
  59.         {
  60.             while (isspace(*nm)) nm++;
  61.             for (p=nm;(*p != '!') && (*p != '+') && 
  62.                 (*p != '-') && (!isspace(*p));p++);
  63.             *p='\0';
  64.         }
  65.         else dt="0";
  66.  
  67.         if (*nm == ';') continue;
  68.  
  69.         loginf("sending bark request for \"%s\", password \"%s\", update \"%s\"",
  70.             nm,pw,dt);
  71.         if ((rc=send_bark())) break;
  72.     }
  73.     if (rc == 0) PUTCHAR(ETB);
  74.     fclose(fp);
  75.     if (rc == 0) unlink(fn);
  76.     return rc;
  77. }
  78.  
  79. SM_DECL(send_bark,"sendbark")
  80. SM_STATES
  81.     send,waitack,getfile
  82. SM_NAMES
  83.     "send","waitack","getfile"
  84. SM_EDECL
  85.  
  86.     int c;
  87.     char buf[256];
  88.     unsigned short crc;
  89.     int count=0;
  90.  
  91.     sprintf(buf,"%s %s %s",nm,dt,pw);
  92.     crc=crc16(buf,strlen(buf));
  93.     debug(10,"sending bark packet \"%s\", crc=0x%04x",buf,crc);
  94.  
  95. SM_START(send)
  96.  
  97. SM_STATE(send)
  98.  
  99.     if (count++ > 6)
  100.     {
  101.         loginf("bark request failed");
  102.         SM_ERROR;
  103.     }
  104.  
  105.     PUTCHAR(ACK);
  106.     PUT(buf,strlen(buf));
  107.     PUTCHAR(ETX);
  108.     PUTCHAR(crc&0xff);
  109.     PUTCHAR((crc>>8)&0xff);
  110.     if (STATUS) {SM_ERROR;}
  111.     else {SM_PROCEED(waitack);}
  112.  
  113. SM_STATE(waitack)
  114.  
  115.     c=GETCHAR(15);
  116.     if (c == TIMEOUT)
  117.     {
  118.         debug(10,"sendbark got timeout waiting for ACK");
  119.         SM_PROCEED(send);
  120.     }
  121.     else if (c < 0)
  122.     {
  123.         SM_PROCEED(send);
  124.     }
  125.     else if (c == ACK)
  126.     {
  127.         SM_PROCEED(getfile);
  128.     }
  129.     else if (c == NAK)
  130.     {
  131.         SM_PROCEED(send);
  132.     }
  133.     else
  134.     {
  135.         debug(10,"sendbark got %s waiting for ACK",
  136.             printablec(c));
  137.         SM_PROCEED(send);
  138.     }
  139.  
  140. SM_STATE(getfile)
  141.  
  142.     switch (xmrecv(NULL))
  143.     {
  144.     case 0:    SM_PROCEED(getfile); break;
  145.     case 1:    SM_SUCCESS; break;
  146.     default: SM_ERROR; break;
  147.     }
  148.  
  149. SM_END
  150. SM_RETURN
  151.